package com.donica.wamstest.Controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.donica.wamstest.common.CheckToken;
import com.donica.wamstest.entity.CommonFindVO;
import com.donica.wamstest.entity.ReturnMessage;
import com.donica.wamstest.entity.UserVO;
import com.donica.wamstest.service.LogService;
import com.donica.wamstest.service.UserService;
import com.donica.wamstest.utils.LogUtil;

import io.swagger.annotations.Api;


/*
 * @author laifuying
 * @version 1.0
 * @date  2020/2/8 19:12
 * @description 用户控制器
 */
@RestController
@RequestMapping("/api/v1/")
@Api(value = "userController", tags = "用户控制器")
public class UserController {
	
	@Autowired
    private UserService userService;
	
	@Autowired
    private LogService logService;
	
    @CheckToken
    @RequestMapping(value = "roles", method = RequestMethod.GET)
    public ReturnMessage getRoles() {
        return userService.getRoles();
    }
    
    @RequestMapping(value = "permissions", method = RequestMethod.POST)
    public ReturnMessage getPermission(@RequestBody UserVO vo) {
        return userService.getPermission(vo);
    }
    
    // 登录
   // @LoginToken
  	@RequestMapping(value = "login", method = RequestMethod.POST)
  	public ReturnMessage login(@RequestBody UserVO vo) {
  		ReturnMessage rtnMsg = userService.login(vo);
  		vo.setPassword("*");
  		logService.SaveLog(LogUtil.ACTION_LOGIN, rtnMsg.getCode(),rtnMsg.getMsg(), vo);
  		return rtnMsg;
  	}


    // 添加用户
 	@RequestMapping(value = "users", method = RequestMethod.POST)
 	public ReturnMessage InsertUser(@RequestBody List<UserVO> volist) {
 		ReturnMessage rtnMsg = new ReturnMessage();
 		try {

 			long count = userService.InsertUser(volist);
 			rtnMsg.setResult(count);
 		} catch (Exception e) {
 			rtnMsg.setCode(ReturnMessage.FAIL);
 			rtnMsg.setMsg(e.getMessage());
 		}
 		logService.SaveLog(LogUtil.ACTION_ADD_USER, rtnMsg.getCode(),rtnMsg.getMsg(), volist);
 		return rtnMsg;
 	}

 	@RequestMapping(value = "users", method = RequestMethod.PUT)
 	public ReturnMessage UpdateUser(@RequestBody List<UserVO> volist) {
 		ReturnMessage rtnMsg = new ReturnMessage();
 		try {

 			long count = userService.UpdateUser(volist);
 			rtnMsg.setResult(count);
 		} catch (Exception e) {
 			rtnMsg.setCode(ReturnMessage.FAIL);
 			rtnMsg.setMsg(e.getMessage());
 		}
 		
 		logService.SaveLog(LogUtil.ACTION_UPDATE_USER, rtnMsg.getCode(),rtnMsg.getMsg(), volist);
 		return rtnMsg;
 	}

 	@RequestMapping(value = "users", method = RequestMethod.DELETE)
 	public ReturnMessage DeleteUser(@RequestBody List<UserVO> volist) {
 		ReturnMessage rtnMsg = new ReturnMessage();
 		try {

 			long count = userService.deleteUser(volist);
 			rtnMsg.setResult(count);
 		} catch (Exception e) {
 			rtnMsg.setCode(ReturnMessage.FAIL);
 			rtnMsg.setMsg(e.getMessage());
 		}

 		logService.SaveLog(LogUtil.ACTION_DELETE_USER, rtnMsg.getCode(),rtnMsg.getMsg(), volist);
 		return rtnMsg;
 	}

 	@RequestMapping(value = "users/list/{pageSize}/{currPage}", method = RequestMethod.POST)
 	public ReturnMessage searchUser(@PathVariable("pageSize") int pageSize, @PathVariable("currPage") int currPage,
 			@RequestBody CommonFindVO vo) {
 		int firstIndex = pageSize * currPage;
 		vo.setFirstIndex(firstIndex);
 		vo.setPageSize(pageSize);
 		vo.setCurrPage(currPage);
 		return userService.searchUser(vo);
 	}

}
